package com.lsfb.duishangbaos.MoneyManage;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.lsfb.duishangbaos.R;
import com.lsfb.duishangbaos.Utils.LittleUtils;
import com.lsfb.duishangbaos.Utils.NoDoubleClickUtils;
import com.lsfb.duishangbaos.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.SN;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.HashMap;

import static com.lsfb.duishangbaos.R.id.aty_btn_post;

/**
 * Created by Administrator on 2017/5/10 0010.
 */
@ContentView(R.layout.aty_draw_money)
public class DrawMoneyActivity extends LsfbActivity {

    /**
     * 提现金额
     */
    @ViewInject(R.id.aty_et_money)
    private EditText et_money;

    /**
     * 备注
     */
    @ViewInject(R.id.aty_et_beizhu)
    private EditText et_beizhu;

    /**
     * 支付宝账号
     */
    @ViewInject(R.id.aty_et_id)
    private EditText et_id;

    /**
     * 支付宝名字
     */
    @ViewInject(R.id.aty_et_name)
    private EditText et_name;

    @Override
    public void initView() {
        LittleUtils.setsimpletitlebar(this, "提现");
    }

    @Override
    public void initEvent() {
        et_money.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable edt) {
                String temp = edt.toString();
                int posDot = temp.indexOf(".");
                if (posDot <= 0) return;
                if (temp.length() - posDot - 1 > 2) {
                    edt.delete(posDot + 3, posDot + 4);
                }
            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }
        });
    }

    @OnClick({aty_btn_post})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.aty_btn_post:
                //收起软键盘
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(et_money.getWindowToken(), 0);
                imm.hideSoftInputFromWindow(et_beizhu.getWindowToken(), 0);
                imm.hideSoftInputFromWindow(et_id.getWindowToken(), 0);
                imm.hideSoftInputFromWindow(et_name.getWindowToken(), 0);
                if (!NoDoubleClickUtils.isDoubleClick()) {
                    postDraw();
                }
                break;
        }
    }

    @Override
    public void initData() {

    }

    public void postDraw() {

        if (et_money.getText().toString().isEmpty()) {
            SN.SnackShow(et_money, "请输入提现金额");
            return;
        }
        if (et_id.getText().toString().isEmpty()) {
            SN.SnackShow(et_money, "请输入支付宝账号");
            return;
        }
        if (et_name.getText().toString().isEmpty()) {
            SN.SnackShow(et_money, "请输入收款人姓名");
            return;
        }

        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("cla", "1");//1商家 ，2用户
        hashMap.put("cash", et_money.getText().toString());//金额
        hashMap.put("paccout", et_id.getText().toString());//账号
        hashMap.put("name", et_name.getText().toString());//账号名字
        if (!et_beizhu.getText().toString().isEmpty()) {
            hashMap.put("bewrite", et_beizhu.getText().toString());//备注
        }
        new BaseInternet().getData(URLString.POSTDRAW, hashMap, DrawMoneyBean.class, new DrawMoneyEvent<DrawMoneyBean>(), true);
    }

    @EventAnnotation
    public void DrawMoneyEvent(DrawMoneyEvent<DrawMoneyBean> event) {
        switch (event.getCode()) {
            case 200:
                T.showShort(this, event.getMsg());
                finish();
                break;
            default:
                SN.SnackShow(et_money, event.getMsg());
                break;
        }
    }

}
